package fic.storefinder.server.updateservice.poi;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.TreeMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import fic.storefinder.shared.utils.Constants;

public class GetPOIServiceXml extends GetPOIService {

	@Override
	public String formatResponse(TreeMap<String, Object> responseData) throws Exception {
		
		DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
		Document doc = docBuilder.newDocument();

		// create the root element and add it to the document
		Element response = doc.createElement(Constants.XML_NODE_RESPONSE);
		doc.appendChild(response);

		@SuppressWarnings("unchecked")
		ArrayList<HashMap<String, Object>> pois = (ArrayList<HashMap<String, Object>>) responseData.get(Constants.JSON_KEY_RESPONSE);
		for (HashMap<String, Object> hashMap : pois) {
			Element poi = doc.createElement(Constants.XML_NODE_POI);
			
			Element element = doc.createElement(Constants.JSON_KEY_POI_ID);
			Text text = doc.createTextNode(((Integer)hashMap.get(Constants.JSON_KEY_POI_ID)).intValue() + "");
			element.appendChild(text);
			poi.appendChild(element);
			
			element = doc.createElement(Constants.JSON_KEY_POI_LON);
			text = doc.createTextNode(((Double)hashMap.get(Constants.JSON_KEY_POI_LON)).doubleValue() + "");
			element.appendChild(text);
			poi.appendChild(element);
			
			element = doc.createElement(Constants.JSON_KEY_POI_LAT);
			text = doc.createTextNode(((Double)hashMap.get(Constants.JSON_KEY_POI_LAT)).doubleValue() + "");
			element.appendChild(text);
			poi.appendChild(element);
			
			element = doc.createElement(Constants.JSON_KEY_POI_STATUS);
			text = doc.createTextNode(((Integer)hashMap.get(Constants.JSON_KEY_POI_STATUS)).intValue() + "");
			element.appendChild(text);
			poi.appendChild(element);
			
			element = doc.createElement(Constants.JSON_KEY_POI_CITY);
			text = doc.createTextNode((String)hashMap.get(Constants.JSON_KEY_POI_CITY));
			element.appendChild(text);
			poi.appendChild(element);
			
			element = doc.createElement(Constants.JSON_KEY_POI_ADDRESS);
			text = doc.createTextNode((String)hashMap.get(Constants.JSON_KEY_POI_ADDRESS));
			element.appendChild(text);
			poi.appendChild(element);
			
			element = doc.createElement(Constants.JSON_KEY_POI_PHONE);
			text = doc.createTextNode((String)hashMap.get(Constants.JSON_KEY_POI_PHONE));
			element.appendChild(text);
			poi.appendChild(element);
			
			element = doc.createElement(Constants.JSON_KEY_POI_NAME);
			text = doc.createTextNode((String)hashMap.get(Constants.JSON_KEY_POI_NAME));
			element.appendChild(text);
			poi.appendChild(element);
			
			element = doc.createElement(Constants.JSON_KEY_POI_ZIPCODE);
			text = doc.createTextNode((String)hashMap.get(Constants.JSON_KEY_POI_ZIPCODE));
			element.appendChild(text);
			poi.appendChild(element);
			
			response.appendChild(poi);
		}

		TransformerFactory transfac = TransformerFactory.newInstance();
		Transformer trans = transfac.newTransformer();
		trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
		trans.setOutputProperty(OutputKeys.INDENT, "yes");

		// create string from xml tree
		StringWriter sw = new StringWriter();
		StreamResult result = new StreamResult(sw);
		DOMSource source = new DOMSource(doc);
		trans.transform(source, result);
		String xmlString = sw.toString();

		return xmlString;
	}

}
